<?
include_once('appointment.php');
include_once('paging.class.php');


function getStaffData($id) {
	$query = "select * from staff where staff_id='$id'";
	$res = query($query);
	$obj = mysql_fetch_object($res);
	return $obj;
}

function getCustomerData($id) {
	$query = "select * from customer_info where customer_id='$id'";
	$res = query($query);
	$obj = mysql_fetch_object($res);
	return $obj;
}

function appointment_grid($from="", $to="", $status = "", $service = "", $staff = "", $customer = "")
{
	
	$business_id = $_SESSION['business_id'];
	$where = " where app.business_id = '$business_id' ";
	
	$start = $_GET['start'];
	
	if($start == '') {
		$start = 1;
	}
	
	$limit = 15;
	$end = ($start + $limit - 1);
	
	if($status != '') {
		$where = $where . " and app.apptstatus='$status'";
	}

	if($service != '') {
		$where = $where . " and app.service_id='$service'";
	}

	if($customer != '') {
		$where = $where . " and app.customer_id='$customer'";
	}
	
	if($staff!= '') {
		$where = $where . " and app.staff_id='$staff'";
	}
	
	if($from != "" or $to !="") {
		$where = $where . " and (app.apptdate between '".$from."' and '".$to."' )";
	}
	
	$where = $where . getAuthWhere();
	
		$count_query = "select count(app.appt_id) as total from finalize_appointment as app 
			  left join customer_info as cus on app.customer_id=cus.customer_id
			  left join staff as st on st.staff_id=app.staff_id
			  left join services as ser on ser.service_id=app.service_id
			  left join appointment_status_type as stat on stat.as_id = app.apptstatus
			$where ";
	$total_res = query($count_query);
	$rows = mysql_fetch_object($total_res);
	$count = $rows->total;
	
	$where = $where . " order by (starttime / apptstatus /cus.customer_id/ser.service_id) DESC LIMIT ".($start-1).", $limit";
	
	
	$query = "select app.*,
			  st.first_name, st.last_name,ser.title,
			  stat.color,stat.description,
			  cus.firstname as cusfirst, cus.lastname as cuslast
			  from finalize_appointment as app 
			  left join customer_info as cus on app.customer_id=cus.customer_id
			  left join staff as st on st.staff_id=app.staff_id
			  left join services as ser on ser.service_id=app.service_id
			  left join appointment_status_type as stat on stat.as_id = app.apptstatus
			$where ";
	$res = query($query);
	
	$page = new paging($start, $count, $limit);
	$page->url = $page->url."?fromdate=$from&to=$to&status_id=$status&customer_id=$customer&service_id=$service&staff_id=$staff";
		
	?>
	<table width="100%"  background-color ="#ffffff">
	  <tr>
		<td><?=$page->pagingText()?></td>
	  </tr>
	</table>
<br />
	<table width="100%" border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#FFDBCA" class="table" background-color ="#ffffff">
     <tr align="left">
	 	<td class="thead" nowrap width="1%" align="center">S.no.</td>
		<td class="thead" nowrap align="center">Customer</td>
		<td class="thead" nowrap align="center">Staff</td>
		<td class="thead" nowrap align="center">Time</td>
		<td class="thead" nowrap align="center">Status</td>
	</tr>
	<?
	if($count > 0) {
		results($res,$start);
	} else {
		noresults();
	}
	?>
	</table>
	<table width="100%" border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#FFDBCA" class="table" background-color ="#ffffff"  >
	  <tr>
		<td><div id="paging">
		<? echo  $page->getLinks(); ?>
		</div></td>
	  </tr>
	</table>
	<?
}

function noresults() {
	?>
	<tr>
		<td class="smallText" align="center" colspan="10">No Appointment found within the selected dates.</td>
	</tr>
	<?
}

function results($res,$start) {
	while($row = mysql_fetch_object($res)) {
	$time = date("d-M-y h:i",$row->starttime)." - " . date("h:i",$row->endtime);
	$customer = substr($row->cusfirst." ".$row->cuslast,0,15);
	$staff = substr($row->first_name." ".$row->last_name,0,15);
	$status = '<font color="'.$row->color.'">'.$row->description.'</font>';
	?>
	<tr id="row-<?=$row->appt_id?>" onclick="openDetails(this.id,'<?=$_SESSION['customer_id']?>')" onmouseover="colorswap(this,1)" onmouseout="colorswap(this,0)">
		<td class="smallText" align="center"><?=$start++?></td>
		<td class="smallText"><?=$customer?></td>
		<td class="smallText"><?=$staff?></td>
		<td class="smallText"><?=$time?></td>
		<td class="smallText"><?=$status?></td>
	</tr>
	<?
	}
}

function isAuth($id) {
	
}

function appDetails($id) {
	$app = getAppointment($id);
	?>
	<table width="100%" border="0" class="table">
  <tr>
    <td width="25%" align="right" valign="top" bgcolor="#f3f3f3">Appointment ID </td>
    <td width="75%" valign="top" bgcolor="#f3f3f3">: <?=$app->appt_id?></td>
  </tr>
  <tr>
    <td align="right" valign="top">Customer Name </td>
    <td valign="top">: <?=$app->cusfirst." ".$app->cuslast?></td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#f3f3f3">Date</td>
    <td valign="top" bgcolor="#f3f3f3">: 
	<?
		$time = date("d-M-y h:i",$app->starttime)." - " . date("h:i",$app->endtime);
		echo $time;
	?>	</td>
  </tr>
  <tr>
    <td align="right" valign="top">Service</td>
    <td valign="top">: <?=$app->title?></td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#f3f3f3">Assigned To </td>
    <td valign="top" bgcolor="#f3f3f3">: <?=$app->first_name." ".$app->last_name?></td>
  </tr>
  <tr>
    <td align="right" valign="top">Status</td>
    <td valign="top">: <?=$app->description?></td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#f3f3f3">Cost</td>
    <td valign="top" bgcolor="#f3f3f3">: <?=$app->cost?></td>
  </tr>
  <tr>
    <td align="right" valign="top">Tip</td>
    <td valign="top">: <?=$app->tip?></td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#f3f3f3">&nbsp;</td>
    <td valign="top" bgcolor="#f3f3f3">&nbsp;</td>
  </tr>
  <tr>
    <td align="right" valign="top">Instructions for customer </td>
    <td valign="top">: <?=$app->specialinstruction?></td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#f3f3f3">Appointment Notes </td>
    <td valign="top" bgcolor="#f3f3f3">: <?=$app->apptnotes?></td>
  </tr>
  <tr>
    <td align="right" valign="top">Customer Notes(internal) </td>
    <td valign="top">:  <?=$app->customernotes?></td>
  </tr>
</table>
	<?
	
}

function getAuthWhere()
{
	$logintype = $_SESSION['logintype'];
	
	if($logintype=="staff") {
		$authWhere = " and st.staff_id='".$_SESSION['staff_id']."'";
	} else if($logintype=="user") {
		$authWhere = " and cus.customer_id='".$_SESSION['customer_id']."'";
	}

	return $authWhere;
}

function getAppointment($id) {
	
		$auth = getAuthWhere();
		
		$query = "select app.*,
			  st.first_name, st.last_name,ser.title,
			  stat.color,stat.description,
			  cus.firstname as cusfirst, cus.lastname as cuslast
			  from finalize_appointment as app 
			  left join customer_info as cus on app.customer_id=cus.customer_id
			  left join staff as st on st.staff_id=app.staff_id
			  left join services as ser on ser.service_id=app.service_id
			  left join appointment_status_type as stat on stat.as_id = app.apptstatus
			where app.appt_id='$id' $auth";
		$res = query($query);
		$row = mysql_fetch_object($res);
		return $row;
}
?>